package com.dwin.dingnuo.views;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.dwin.dingnuo.R;


/**
 * 自定义UI Dialog
 */
public class DwinInputDialogV2 extends Dialog implements View.OnClickListener {

    private Context context;
    private TextView dialog_tip;
    private TextView tv_positive;
    private MyDialogListener listener;

    private EditText editText;
    //    private ImageView ivDelete;
    private String title;
    private String name;
    private String nameHint;

    public DwinInputDialogV2 setMyTitle(String title) {
        this.title = title;
        return this;
    }

    public DwinInputDialogV2 setMyName(String name) {
        this.name = name;
        return this;
    }

    public DwinInputDialogV2 setMyNameHint(String nameHint) {
        this.nameHint = nameHint;
        return this;
    }

    public interface MyDialogListener {
        void onClickPositive(String name);
    }

    public DwinInputDialogV2(Context context) {
        super(context);
        this.context = context;
    }

    public DwinInputDialogV2(Context context, int style, MyDialogListener listener) {
        super(context, style);
        this.context = context;
        this.listener = listener;
    }

    //    style引用style样式  dialog_update_layout
    public DwinInputDialogV2(Context context, int style) {
        super(context, style);
    }

    public void setDialogListener(MyDialogListener listener) {
        this.listener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_input_layout_v2);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);

        dialog_tip = findViewById(R.id.dialog_tip);
        dialog_tip.setText(title);
        editText = findViewById(R.id.edit_text);
        editText.setHint(nameHint);
        if (!TextUtils.isEmpty(name)) {
            editText.setText(name);
            editText.setSelection(name.length()); // 光标到最后面
        }

//        ivDelete = findViewById(R.id.iv_delete);
//        ivDelete.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                editText.setText("");
//            }
//        });
        tv_positive = (TextView) findViewById(R.id.tv_positive);
        tv_positive.setOnClickListener(this);

//        editText.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                if (s.length() > 0) {
//                    ivDelete.setVisibility(View.VISIBLE);
//                } else {
//                    ivDelete.setVisibility(View.GONE);
//                }
//            }
//        });
    }

    @Override
    public void onClick(View v) {
        if (listener != null) {
            switch (v.getId()) {
                case R.id.tv_positive:
                    String name = editText.getText().toString().trim();
                    listener.onClickPositive(name);
                    break;
            }
        }
    }

}
